$(function () {
    var catagory_table;
    layui.use('table',function () {
        var table = layui.table;
        catagory_table = table.render({
            elem: '#catagory_table'
            ,height: 625 //容器高度
            ,cols: [[
                {fixed:'left',type:'checkbox'},
                {field: 'id', hide:true},
                {field: 'name',title:'分类名称'},
                {field: 'createTime',title:'创建时间'}
            ]] //设置表头
            ,toolbar:true
            ,page:true
            ,url:'/table/list/catagory'
        });
    })

    $("#catagory_insert_btn").click(function () {
        layer.open({ //变成方法需要五个参数，一个返回值，中间也有不同的格式检验方法
            type: 1,
            title:'分类添加',
            skin: 'layui-layer-rim', //加上边框
            area: ['666px', '450px'], //宽高
            content: $("#catagory_model").html(),
            shadeClose:true,
            tipsMore:true,
            btnAlign: 'c',
            btn: ['确认添加', '取消']
            ,btn1: function(index, layero){
                if(valid_input()){
                    var params = $("#catagory_form").serialize();
                    $.ajax({
                        type:"post",
                        url:'/catagory/insert',
                        data:params,
                        success:function (r) {
                            if(r=='success'){
                                layer.open({
                                    icon:1,
                                    title:"success",
                                    content: "添加成功",
                                    shadeClose:true
                                });
                                catagory_table = table_reload_insert(catagory_table,"catagory_table");
                            }else{
                                layer.open({
                                    icon:2,
                                    title:"fail",
                                    content: r,
                                    shadeClose:true
                                });
                                catagory_table = table_reload(catagory_table,"catagory_table");
                            }
                            if(r!="已经有此分类了"){
                                setTimeout(function () { parent.layer.close(index) }, 100);//延迟
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    })
                }
            }
        });
        layui.use('form', function(){
            var form = layui.form;
            form.render();
        });
    })

    $("#catagory_delete_btn").click(function () {
        layui.use('table',function () {
            var table = layui.table;
            var checkStatus = table.checkStatus('catagory_table');
            var data = checkStatus.data;
            if(data.length>0){
                layer.confirm('真的删除麽',function () {
                    $.ajax({
                        type:"post",
                        url:'/catagory/delete',
                        dataType:"json",
                        contentType: "application/json;charset=utf-8",
                        data:JSON.stringify(data),
                        success:function (r) {
                            if(r.status='success'){
                                layer.msg("删除成功");
                            }else{
                                layer.msg("error");
                            }
                            catagory_table = table_reload_del(catagory_table,"catagory_table");
                        }
                    })

                })
            }else{
                layer.msg("请选中至少一条数据");
            }
        })
    })

    $("#catagory_update_btn").click(function () {
        layui.use('table',function () {
            var table = layui.table;
            var checkStatus = table.checkStatus('catagory_table');
            var data = checkStatus.data;
            if(data.length==1){
                layer.open({ //变成方法需要五个参数，一个返回值，中间也有不同的格式检验方法
                    type: 1,
                    title:'分类修改',
                    skin: 'layui-layer-rim', //加上边框
                    area: ['666px', '450px'], //宽高
                    content: $("#catagory_model").html(),
                    shadeClose:true,
                    btnAlign: 'c',
                    btn: ['确认修改', '取消']
                    ,btn1: function(index, layero){
                        if(valid_input()){
                            var params = $("#catagory_form").serialize();
                            $.ajax({
                                type:"post",
                                url:'/catagory/update',
                                data:params,
                                success:function (r) {
                                    if(r=='success'){
                                        layer.open({
                                            icon:1,
                                            title:"success",
                                            content: "修改成功",
                                            shadeClose:true
                                        });
                                    }else{
                                        layer.open({
                                            icon:2,
                                            title:"fail",
                                            content: r,
                                            shadeClose:true
                                        });
                                    }
                                    catagory_table = table_reload(catagory_table,"catagory_table");
                                    if(r!="已经有此分类了"){
                                        setTimeout(function () { parent.layer.close(index) }, 100);//延迟
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    console.log(XMLHttpRequest.status);
                                    console.log(XMLHttpRequest.readyState);
                                    console.log(textStatus);
                                }
                            })
                        }
                    },
                    success:function () {
                        $("#id").val(data[0].id);
                        $("#name").val(data[0].name);
                    }
                });
                layui.use('form', function(){
                    var form = layui.form;
                    form.render();
                });
            }else{
                layer.msg("请选中一条数据");
            }
        })
    })


})

function valid_input() {
    var name = $("#name").val();
    if(isNull(name)){
        layer.msg("请输入分类名称");
        return false;
    }
    return true;
}

